package andestours.interfaz;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class ServletTemplate extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		procesarPedido(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		procesarPedido(request, response);
	}
	
	private void procesarPedido(HttpServletRequest request, HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();
			imprimirHeader(out);
			imprimirContenido(request, out);
			imprimirFooter(out);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

	private void imprimirHeader(PrintWriter out) {
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
		out.println("<head>");
		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />");
		out.println("<title>AndesTours</title>");
		out.println("<script language=\"javascript\">");
		out.println("function seleccionarTodos() {");
		out.println("		document.forms[0].todos.disabled = false;");
		out.println("		document.forms[0].visitados.selectedIndex = '0';");
		out.println("		document.forms[0].visitados.disabled = true;");
		out.println("}");
		out.println("function seleccionarVisitados() {");
		out.println("		document.forms[0].visitados.disabled = false;");
		out.println("		document.forms[0].todos.selectedIndex = '0';");
		out.println("		document.forms[0].todos.disabled = true;	");	
		out.println("}");
		out.println("function seleccionar() {");
		out.println("		window.open('plan.html?masVisitados=1','plan de ejecucion','width=200,'height=200');");
		out.println("		document.forms[0].submit();");
		out.println("}");
		out.println("function escogerServicio() { ");
		out.println("		document.forms[1].submit();");
		out.println("}");
		out.println("function validarInicio() {");
		out.println("	if(document.forms[1].codigo.value == '0') {");
		out.println("		alert('Por favor seleccione un destino para continuar.')");
		out.println("		return false;");
		out.println("	}");
		out.println("	else return true;");
		out.println("}");
		out.println(" function validarEstadia() { ");
		out.println(" 	var f = document.forms[0]; ");
		out.println(" 	if( !filtro(f.dinero)) { ");
		out.println(" 		alert('Por favor llene todos los campos correctamente.'); ");
		out.println(" 		return false; ");
		out.println(" 	} ");
		out.println(" 	return true; ");
		out.println(" } ");
		out.println(" function filtro(field) { ");
		out.println("           var check = true; ");
		out.println("           if(field.value == '') ");
		out.println("           		return false; ");
		out.println("           for(var i=0; i<field.value.length; ++i) { ");
		out.println("                var new_key = field.value.charAt(i); ");
		out.println("                if(((new_key < '0') || (new_key > '9')) && !(new_key == '')) ");
		out.println("                     return false; ");
		out.println("           } ");
		out.println("           return true; ");
		out.println("      } ");
		out.println(" function validarTransporte() { ");
		out.println(" if( !filtro(document.forms[0].costo))  { ");
		out.println(" 	alert('Por favor escriba el costo maximo.'); ");
		out.println(" 	return false; ");
		out.println(" } ");
		out.println(" return true; ");
		out.println(" } ");
		out.println(" function validarReserva() { ");
		out.println(" if( document.forms[0].nombre.value == '' || !filtro(document.forms[0].tarjeta) || document.forms[0].nombre.value.indexOf(' ') == -1) { ");
		out.println(" 	alert('Por favor llene todos los campos correctamente.'); ");
		out.println(" 	return false; ");
		out.println(" } ");
		out.println(" return true; ");
		out.println(" } ");
		out.println(" function hotel() { ");
		out.println(" 	document.forms[0].tipo.disabled = true; ");
		out.println(" 	document.forms[0].origen.disabled = true; ");
		out.println(" 	document.forms[0].estrellas.disabled = false; ");
		out.println(" } ");
		out.println(" function transporte() { ");
		out.println(" 	document.forms[0].tipo.disabled = false; ");
		out.println(" 	document.forms[0].origen.disabled = false; ");
		out.println(" 	document.forms[0].estrellas.disabled = true; ");
		out.println(" } ");
		out.println(" function validarAdmin() { ");
		out.println(" 	var f = document.forms[0]; ");
		out.println(" 	if( !filtro(f.codigo) || !filtro(f.costo) || !filtro(f.capacidad) || f.nombre.value == '') { ");
		out.println(" 		alert('Por favor llene todos los campos correctamente.'); ");
		out.println(" 		return false; ");
		out.println("		} ");
		out.println(" 		return true;");
		out.println(" } ");
		out.println("</script>");
		out.println("<style>a:hover {color:blue;}</style>");
		out.println("</head>");		
		out.println("<body>");
		out.println("<div align=\"right\">");
		out.println("<font face='Verdana, Arial, Helvetica, sans-serif' size='1'><a href='' onclick=\"javascript:if(prompt('Introduzca su password de Administrador','') == 'tourcito') {window.open('./administrador.html')} else {alert('Ups! Password invalido.')}\" style=\"text-decoration:none;color:#aaaaaa;\">ADMIN</a></font>");
		out.println("</div>");
		out.println("<table width=\"100%\" height=\"100%\" cellpadding=\"50\">");
		out.println("    <tr>");
		out.println("        <td colspan=\"2\">");
		out.println("			<center>");
		out.println("				<a href='./index.html' style='text-decoration:none'><font size=\"+6\" color=\"#6666aa\" face=\"Verdana, Arial, Helvetica, sans-serif\">AndesTours</font></a>");
		out.println("				<br />");
		out.println("				<font face=\"Verdana, Arial, Helvetica, sans-serif\">Su compa&ntilde;ero de viajes</font>");
		out.println("			</center>");
		out.println("		</td> ");
		out.println("    </tr>");
	}
	
	public abstract void imprimirContenido(HttpServletRequest request, PrintWriter out) throws Exception;
	
	private void imprimirFooter(PrintWriter out) {
		out.println("</table><br /><center>");
		out.println("<font size='1' color='#aaaaaa' face='Verdana, Arial, Helvetica, sans-serif'><a style='text-decoration:none;color:#aaaaaa' href='#'>About Us</a> | <a style='text-decoration:none;color:#aaaaaa' href='#'>Site Map</a> | <a style='text-decoration:none;color:#aaaaaa' href='#'>Privacy Policy</a> | <a style='text-decoration:none;color:#aaaaaa' href='#'>Contact Us</a> | &copy;2009 AndesTours Inc. </div>");
		out.println("</body>");
		out.println("</html>");
	}
}
